package sz.szsmk.citizencard.ui.newversion.activity

import android.app.ActivityManager
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.ViewPager
import com.chenenyu.router.annotation.Route
import com.flyco.tablayout.listener.CustomTabEntity
import com.gyf.immersionbar.ktx.immersionBar
import kotlinx.android.synthetic.main.activity_qrcode.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import sz.szsmk.citizencard.R
import sz.szsmk.citizencard.model.network.bean.InBackgroundBean
import sz.szsmk.citizencard.ui.newversion.bean.TabEntity
import sz.szsmk.citizencard.ui.newversion.config.Constants
import sz.szsmk.citizencard.ui.newversion.fragment.adapter.ScanQRTabAdapter
import sz.szsmk.citizencard.util.JumpUtils
import sz.szsmk.citizencard.util.ScrennLight
import java.util.*

/**
 *  author : hyt
 *  date : 2020/9/2
 *  description : 扫码页面
 */
@Route(Constants.QRCODEACTIVITY)
class ScanQRCodeActivity : sz.szsmk.citizencard.ui.dzsmkavtivity.AppBaseActivity() {

    var ic_chengche : Drawable?=null
    var ic_shenfen  :Drawable ?=null
    var ic_zhifu :Drawable ?=null

    private val mIconUnselectIds = intArrayOf(
            R.mipmap.ic_chengche, R.mipmap.ic_shenfen,
            R.mipmap.ic_zhifu)

    private var mTabEntities = ArrayList<CustomTabEntity>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_qrcode)

        //沉浸
        immersionBar{
            fitsSystemWindows(true)
            statusBarColor(R.color.white)
            statusBarDarkFont(true)
        }
        //不能截屏
        window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
        ScrennLight.setBrightness(this,255)


        back.setOnClickListener { finish() }

        val mTitles = arrayOf(
                getString(R.string.carCode),
                getString(R.string.idCode),
                getString(R.string.payCode))

        for (i in mTitles.indices) {
            mTabEntities.add(TabEntity(mTitles[i], mIconUnselectIds.get(i), mIconUnselectIds[i]))
        }

        //几个Fragment 在 CardListTabAdapter 里面
        val sectionsPagerAdapter = ScanQRTabAdapter(this, supportFragmentManager)
        view_pager.offscreenPageLimit = 3
        view_pager.adapter = sectionsPagerAdapter

        back.setOnClickListener { finish() }
        view_pager.addOnPageChangeListener(object:ViewPager.OnPageChangeListener{
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                when(position){
                    0-> onClick(tv_buscode)
                    1-> onClick(tv_idcode)
                    2-> onClick(tv_paycode)
                }
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })


        ic_chengche =  ContextCompat.getDrawable(this, R.mipmap.ic_chengche)
        ic_shenfen =  ContextCompat.getDrawable(this, R.mipmap.ic_shenfen)
        ic_zhifu =  ContextCompat.getDrawable(this, R.mipmap.ic_zhifu)
        tv_buscode.isSelected = true

        //根据参数选择进入方式
        when(intent.getStringExtra(SCAN_TYPE)){
            BUS_CODE -> {
                onClick(tv_buscode)
            }
            ID_CODE -> {
                onClick(tv_idcode)
            }
            PAY_CODE -> {
                onClick(tv_paycode)
            }
            else->{
                onClick(tv_buscode)
            }
        }

        tv_accountSecurity.setOnClickListener {
            JumpUtils.jump(this, "psdadmin", 0)
        }
    }

    override fun getTopTitle() = null



    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }


    fun onClick(view: View){
        tv_buscode.isSelected = false
        tv_idcode.isSelected = false
        tv_paycode.isSelected = false
        tv_buscode.setTextColor(ContextCompat.getColor(this, R.color.color_d3e3f6))
        tv_buscode.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
        tv_buscode.compoundDrawablePadding = 0
        tv_idcode.setTextColor(ContextCompat.getColor(this, R.color.color_d3e3f6))
        tv_idcode.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
        tv_idcode.compoundDrawablePadding = 0
        tv_paycode.setTextColor(ContextCompat.getColor(this, R.color.color_d3e3f6))
        tv_paycode.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
        tv_paycode.compoundDrawablePadding = 0
        when(view.id){
            //乘车码
            R.id.tv_buscode -> {
                tv_buscode.isSelected = true
                tv_buscode.setTextColor(ContextCompat.getColor(this, R.color.color_2184FA))
                tv_buscode.setCompoundDrawablesWithIntrinsicBounds(ic_chengche, null, null, null)
                tv_buscode.compoundDrawablePadding = 5
                view_pager.currentItem = 0
            }
            //身份码
            R.id.tv_idcode -> {
                tv_idcode.isSelected = true
                tv_idcode.setTextColor(ContextCompat.getColor(this, R.color.color_2184FA))
                tv_idcode.setCompoundDrawablesWithIntrinsicBounds(ic_shenfen, null, null, null)
                tv_idcode.compoundDrawablePadding = 5
                view_pager.currentItem = 1
            }
            //支付码
            R.id.tv_paycode -> {
                tv_paycode.isSelected = true
                tv_paycode.setTextColor(ContextCompat.getColor(this, R.color.color_2184FA))
                tv_paycode.setCompoundDrawablesWithIntrinsicBounds(ic_zhifu, null, null, null)
                tv_paycode.compoundDrawablePadding = 5
                view_pager.currentItem = 2

            }
        }

    }

    companion object {
        const val SCAN_TYPE = "scan_type"
        const val BUS_CODE = "1000"
        const val ID_CODE = "2000"
        const val PAY_CODE = "3000"
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: InBackgroundBean) {
        if(event!=null){
            finish()
        }
    }
}